//
//  TalkfunTestPaperDetailsHeadView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/1/11.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
enum closeOrBackState {
    //关闭
    case close
    //返回
    case back
}


class TalkfunTestPaperDetailsHeadView: UIView {
    //关闭或返回
    @objc public let closeOrBack:TalkfunIncreaseButton = TalkfunIncreaseButton.init()
    //更多
    @objc public let more:TalkfunIncreaseButton = TalkfunIncreaseButton.init()
    //倒计时
    @objc public let countdown:UILabel = UILabel.init()
    //倒计时定时器
    @objc public var timer:Timer!
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.countdown.isHidden = true
        
        self.closeOrBack.layer.cornerRadius = 2;
        self.addSubview(self.closeOrBack)
        self.closeOrBack.backgroundColor = UIColor.clear
        
        self.closeOrBackStateSwitch(state: closeOrBackState.back)
        
        self.addSubview(self.more)
        self.more.setImage(UIImage.init(named: "HTLive.bundle/TFMore"), for: UIControl.State.normal)
        self.more.setImage(UIImage.init(named: "HTLive.bundle/TFMore"), for: UIControl.State.highlighted)
        
        // 倒计时
        self.addSubview(self.countdown)
        self.countdown.textAlignment = NSTextAlignment.center
        self.countdown.numberOfLines = 0
        self.openCountdown(time: "00:00:00")
    }
    
   
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let closeOrBackY = 15.0
        let closeOrBackW = 15.0
        let closeOrBackX = self.frame.width - 15.0 - 15.0
        let closeOrBackH = 15.0
        self.closeOrBack.frame = CGRect(x: closeOrBackX, y: closeOrBackY, width: closeOrBackW, height: closeOrBackH)
        
        let countdownH = 21.0
        let countdownY = 15.0
        self.countdown.frame = CGRect(x: 0.0, y: countdownY, width: self.frame.width, height: countdownH)
    }
    
    func closeOrBackStateSwitch(state:closeOrBackState)
    {
        if (state == closeOrBackState.close)
        {
            self.closeOrBack.setImage(UIImage.init(named: "HTLive.bundle/examinatioCclose_slices"), for: UIControl.State.normal)
            self.closeOrBack.isSelected = true
        }else if (state == closeOrBackState.back)
        {
            self.closeOrBack.setImage(UIImage.init(named: "HTLive.bundle/TFreturn"), for: UIControl.State.normal)
           
            self.closeOrBack.isSelected = false
        }

    }
    
    //开始倒计时 duration = 3600
    @objc func openCountdown(time:NSString)
    {

        let countdownStr = NSString(format:"倒计时：%@", time)
        let attrString = NSMutableAttributedString(string: countdownStr as String)
        let font =  UIFont(name: "PingFang SC", size: 15) as Any

        let attr: [NSAttributedString.Key : Any] = [.font:font,.foregroundColor: UIColor(red: 0.52, green: 0.53, blue: 0.54,alpha:1), ]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))

        self.countdown.attributedText = attrString
        
 
    }
    @objc func moreTitle(title:NSString , status:TalkfunQuestionnairePageStatus)
    {
        var font = UIFont(name: "PingFang SC", size: 14) as Any
        
        self.more.layerBorderColorView.layer.borderWidth = 0
        self.more.layerBorderColorView.layer.borderColor = UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1).cgColor
        
        self.more.layerBorderColorView.layer.masksToBounds = true
        self.more.layerBorderColorView.layer.cornerRadius = 0
        
        //存在
        if (title.contains("/"))
       {
            font = UIFont(name: "PingFang SC", size: 15) as Any
           let attrString = NSMutableAttributedString(string: title as String)
           let attr: [NSAttributedString.Key : Any] = [.font: font,.foregroundColor: UIColor(red: 0.59, green: 0.63, blue: 0.67,alpha:1), ]
           attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
           
          let array =  attrString.mutableString.components(separatedBy: "/")
          let inx0 = array[0] as NSString
          let inx1 = array[1] as NSString
           
           let strSubAttr1: [NSMutableAttributedString.Key: Any] = [.font: font,.foregroundColor: UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1)]
           attrString.addAttributes(strSubAttr1, range: NSRange(location: 0, length: inx0.length))
           
           let strSubAttr2: [NSMutableAttributedString.Key: Any] = [.font: font,.foregroundColor: UIColor(red: 0.59, green: 0.63, blue: 0.67,alpha:1)]
           attrString.addAttributes(strSubAttr2, range: NSRange(location: inx0.length, length: inx1.length))
            self.more.setAttributedTitle(attrString, for: UIControl.State.normal)
       }else{
           //已交卷 或者 已批阅
           let attrString = NSMutableAttributedString(string: title as String)
               font = UIFont(name: "PingFang SC", size: 14) as Any
          var color = UIColor(red: 0.54, green: 0.61, blue: 0.69,alpha:1)
           if (status == TalkfunQuestionnairePageStatusPagination && title.contains("交卷")) {
               color = UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1)
               self.more.layerBorderColorView.layer.borderWidth = 0.5
               self.more.layerBorderColorView.layer.borderColor = UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1).cgColor
               
               self.more.layerBorderColorView.layer.masksToBounds = true
               self.more.layerBorderColorView.layer.cornerRadius = 4
               
           }else
           {
               
           }
           if(title.contains("答对"))
           {
               color = UIColor(red: 0.05, green: 0.73, blue: 0.25,alpha:1)
           }
           let attr: [NSAttributedString.Key : Any] = [.font: font,.foregroundColor: color, ]
           attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
       
           self.more.setAttributedTitle(attrString, for: UIControl.State.normal)
          
           
       }
        
        
        let size: CGSize = title.size(withAttributes: [NSAttributedString.Key.font: font])
        let moreX = 12.0
        let moreY = moreX
        let moreW = size.width + 22
        let moreH = 21.0
       
        self.more.spacing = 2
        self.more.frame = CGRect(x: moreX, y: moreY, width: moreW, height: moreH)

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
